% % 读入灰度图像
% I = imread('photo.jpg');
% I_gray = rgb2gray(I);
% 
% % Roberts 算子
% Gx_Roberts = [1 0; 0 -1];
% Gy_Roberts = [0 1; -1 0];
% Ix_Roberts = imfilter(double(I_gray), Gx_Roberts);
% Iy_Roberts = imfilter(double(I_gray), Gy_Roberts);
% Edge_Roberts = sqrt(Ix_Roberts.^2 + Iy_Roberts.^2);
% 
% % Sobel 算子
% Gx_Sobel = [-1 0 1; -2 0 2; -1 0 1];
% Gy_Sobel = [-1 -2 -1; 0 0 0; 1 2 1];
% Ix_Sobel = imfilter(double(I_gray), Gx_Sobel);
% Iy_Sobel = imfilter(double(I_gray), Gy_Sobel);
% Edge_Sobel = sqrt(Ix_Sobel.^2 + Iy_Sobel.^2);
% 
% % Prewitt 算子
% Gx_Prewitt = [-1 0 1; -1 0 1; -1 0 1];
% Gy_Prewitt = [-1 -1 -1; 0 0 0; 1 1 1];
% Ix_Prewitt = imfilter(double(I_gray), Gx_Prewitt);
% Iy_Prewitt = imfilter(double(I_gray), Gy_Prewitt);
% Edge_Prewitt = sqrt(Ix_Prewitt.^2 + Iy_Prewitt.^2);
% 
% % 显示结果
% figure;
% subplot(2,2,1), imshow(I), title('Original Image');
% subplot(2,2,2), imshow(Edge_Roberts, []), title('Roberts Edge');
% subplot(2,2,3), imshow(Edge_Sobel, []), title('Sobel Edge');
% subplot(2,2,4), imshow(Edge_Prewitt, []), title('Prewitt Edge');

% 读取RGB图像
rgb_img = imread('photo.jpg');

% 转换为HSI
hsi_img = rgb2hsi(rgb_img);

% 转换回RGB
rgb_converted = hsi2rgb(hsi_img);

% 显示结果
figure;
subplot(1,3,1), imshow(rgb_img), title('原始RGB图像');
subplot(1,3,2), imshow(hsi_img), title('HSI图像');
subplot(1,3,3), imshow(rgb_converted), title('转换回RGB的图像');

function hsi = rgb2hsi(rgb)
    % 将RGB转换为[0, 1]的浮点值
    rgb = im2double(rgb);
    R = rgb(:,:,1);
    G = rgb(:,:,2);
    B = rgb(:,:,3);

    % 初始化HSI通道
    [rows, cols, ~] = size(rgb);
    H = zeros(rows, cols);
    S = zeros(rows, cols);
    I = (R + G + B) / 3;

    % 计算色调（H）
    num = 0.5 * ((R - G) + (R - B));
    den = sqrt((R - G).^2 + (R - B).*(G - B));
    theta = acos(num ./ (den + eps)); % 避免除零

    % 如果B > G，则 H = 360 - theta
    H(B > G) = 2 * pi - theta(B > G);
    H(B <= G) = theta(B <= G);
    H = H / (2 * pi); % 归一化到 [0, 1]

    % 计算饱和度（S）
    min_RGB = min(min(R, G), B);
    S = 1 - (3 ./ (R + G + B + eps)) .* min_RGB; % 避免除零

    % 合并HSI通道
    hsi = cat(3, H, S, I);
end


function rgb = hsi2rgb(hsi)
    % 提取HSI通道
    H = hsi(:,:,1) * 2 * pi; % 转换到 [0, 2pi]
    S = hsi(:,:,2);
    I = hsi(:,:,3);

    % 初始化RGB通道
    [rows, cols, ~] = size(hsi);
    R = zeros(rows, cols);
    G = zeros(rows, cols);
    B = zeros(rows, cols);

    % 区间划分
    % 0 <= H < 2*pi/3
    idx = (0 <= H) & (H < 2*pi/3);
    B(idx) = I(idx) .* (1 - S(idx));
    R(idx) = I(idx) .* (1 + S(idx) .* cos(H(idx)) ./ cos(pi/3 - H(idx)));
    G(idx) = 3*I(idx) - (R(idx) + B(idx));

    % 2*pi/3 <= H < 4*pi/3
    idx = (2*pi/3 <= H) & (H < 4*pi/3);
    R(idx) = I(idx) .* (1 - S(idx));
    G(idx) = I(idx) .* (1 + S(idx) .* cos(H(idx) - 2*pi/3) ./ cos(pi - H(idx)));
    B(idx) = 3*I(idx) - (R(idx) + G(idx));

    % 4*pi/3 <= H < 2*pi
    idx = (4*pi/3 <= H) & (H < 2*pi);
    G(idx) = I(idx) .* (1 - S(idx));
    B(idx) = I(idx) .* (1 + S(idx) .* cos(H(idx) - 4*pi/3) ./ cos(5*pi/3 - H(idx)));
    R(idx) = 3*I(idx) - (G(idx) + B(idx));

    % 合并RGB通道并转换到 [0, 1] 区间
    rgb = cat(3, R, G, B);
end


